package com.wzz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzz.mapper.ProductMapper;
import com.wzz.pojo.Format;
import com.wzz.pojo.Product;
import com.wzz.pojo.vo.PageInfoVo;
import com.wzz.pojo.vo.R;
import com.wzz.service.ProductService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author DarkClown
 * @date 2024-08-02 16:32
 * @project_name week3-try
 * @parem
 **/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper,Product> implements ProductService {
    @Autowired
    ProductMapper productMapper;


    @Override
    public R selectList(PageInfoVo pageInfoVo) {
        //分页
        Page<Product> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());

        //匹配
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(pageInfoVo.getKeyWord())){
            queryWrapper.lambda().like(Product::getProName,pageInfoVo.getKeyWord());
        }
        //日期范围查询
        if (!StringUtils.isEmpty(pageInfoVo.getBeginTime())){
            queryWrapper.lambda().ge(Product::getCreateTime,pageInfoVo.getBeginTime());
        }
        if (!StringUtils.isEmpty(pageInfoVo.getEndTime())){
            queryWrapper.lambda().ge(Product::getCreateTime,pageInfoVo.getEndTime());
        }
        //排序
        queryWrapper.lambda().orderByDesc(Product::getPid);

        Page<Product> selectPage = productMapper.selectPage(page, queryWrapper);

        HashMap<String, Object> map = new HashMap<>();

        map.put("pageSize",pageInfoVo.getPageSize());
        map.put("pageNum",pageInfoVo.getPageNum());
        map.put("total",selectPage.getTotal());
        map.put("records",selectPage.getRecords());


        return R.OK(map);
    }

    @Override
    public R selectFormatList() {
         return R.OK(productMapper.selectForMat());
    }
}
